﻿using Game.NetWork.Packages;
using Game.NetWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Game.Server.Event;
using GameEntitySystem;
using Engine;

namespace Game.Server.Plugins
{
    public class 发帖人 : AbstractProcessCmd
    {
        public override string Cmd => "for";

        public override string Introduce => "循环";

        public override int AuthLevel => 100;

        public override DisplayType Display => DisplayType.All;

        public static bool de { get; set; } = false;

        private object sw = null;

        public static string 循环小消息 = "循环小消息";

        public static float 每循环时间 = 10.0f;

        public override void ProcessCmd()
        {
            if (m_messageDatas.Length > 4 || m_messageDatas.Length < 2)
            {
                DisplaySmallMessageNet广播("参数错误\n 格式：/for [on 1/off 0] [循环小消息] [循环时间]");
            }

            sw = m_messageDatas[1];

            if (sw.ToString() == "0")
            {
                de = false;
                DisplaySmallMessageNet广播("循环小消息已关闭！");
            }
            else if (sw.ToString() == "1")
            {
                de = true;
                DisplaySmallMessageNet广播("循环小消息已开启！");
            }
            else
            {
                DisplaySmallMessageNet广播("非法格式");
            }

            if (sw.ToString() == "on")
            {
                de = true;
                DisplaySmallMessageNet广播("循环小消息已开启！");
            }
            else if (sw.ToString() == "off")
            {
                de = false;
                DisplaySmallMessageNet广播("循环小消息已关闭！");
            }
            else
            {
                DisplaySmallMessageNet广播("非法格式");
            }

            循环小消息 = m_messageDatas[2];
            try
            {
                每循环时间 = float.Parse(m_messageDatas[3]);// 循环时间
            }
            catch (Exception)
            {
                DisplaySmallMessageNet广播("非法格式 float");
            }
        }
        public void DisplaySmallMessageNet广播(string text)  // 小消息方法 广播
        {
            var messagePackget = new MessagePackage("<c=red>系统广播</c>", text, 0, null);
            CommonLib.Net.QueuePackage(messagePackget);
        }

    }
    public class 小消息 : ServerPlugin, ICreatureSpawnEventHandle
    {
        public override int Version => 1;

        public override string Name => "循环弹出小消息插件";

        public byte FirstLevel => 1;

        public static float 循环时间 = 发帖人.每循环时间;

        public static string 循环小消息 = 发帖人.循环小消息;

        public void InitCreatureTypes(SubsystemCreatureSpawn subsystemCreatureSpawn, List<SubsystemCreatureSpawn.CreatureType> creatureTypes) // 注册实体类型
        {

        }

        public override void Initialize()
        {
            CreatureSpawnEventManager.AddObject(this);// 注册生物生成插件
        }

        public override void Load()
        {

        }

        public void OnEntityAdded(SubsystemCreatureSpawn subsystemCreatureSpawn, Entity entity)
        {

        }

        public void OnEntityRemoved(SubsystemCreatureSpawn subsystemCreatureSpawn, Entity entity)
        {

        }

        public void OnPlayerSpawned(PlayerData playerData, Entity playerEntity, Vector3 position)
        {

        }

        public override void Save()
        {

        }

        public bool Update(SubsystemCreatureSpawn subsystemCreatureSpawn, float dt)
        {
            循环时间 = 发帖人.每循环时间;
            循环小消息 = 发帖人.循环小消息;
            if (发帖人.de == true)
            {
                bool flag = subsystemCreatureSpawn.m_subsystemTime.PeriodicGameTimeEvent(循环时间, 0.0); // 周期事件  
                if (flag)
                {
                    var 发帖人 = new 发帖人();// 实例化发帖人
                    发帖人.DisplaySmallMessageNet广播($"{循环小消息.ToString()}"); // 循环小消息
                }
            }
            else
            {

            }
            return true;
        }
    }

}

